extends Panel


@export var target: Character
@export var active: bool

@onready var total_damage_lbl: Label = $MarginContainer/VBoxContainer/HBoxContainer/TotalDamageLbl
@onready var dps_lbl: Label = $MarginContainer/VBoxContainer/HBoxContainer2/DpsLbl
@onready var peak_damage_lbl: Label = $MarginContainer/VBoxContainer/HBoxContainer3/PeakDamageLbl
@onready var damage_attr_lbl: Label = $MarginContainer/VBoxContainer/HBoxContainer4/DamageAttrLbl



var _total_damage: float = 0
var _peak_damage: float = 0
var _dps: float = 0

var _last_time: int = 0
var _total_sec: int = 0


func _ready() -> void:
    target.handle_damage_signals.receive_damage_applied.connect(
        func(info: DamageInfo) -> void:
            # 计算总伤害和峰值伤害
            _total_damage += info.calced_damage
            _peak_damage = max(_peak_damage, info.calced_damage)

            total_damage_lbl.text = "%.1f" % _total_damage
            peak_damage_lbl.text = "%.1f" % _peak_damage
            damage_attr_lbl.text = info.get_damage_attribute_text()
    )


    # 初始化
    reset()


func _process(_delta: float) -> void:
    if not active: return

    # 计算秒伤
    if Time.get_ticks_msec() - _last_time >= 1000:
        _total_sec += 1
        _dps = _total_damage / _total_sec
        if is_zero_approx(_dps):
            dps_lbl.text = "0"
        else:
            dps_lbl.text = "%.1f" % _dps
        _last_time = Time.get_ticks_msec()


func reset() -> void:
    total_damage_lbl.text = "0"
    dps_lbl.text = "0"
    peak_damage_lbl.text = "0"
    damage_attr_lbl.text = "无"

    _total_damage = 0
    _peak_damage = 0
    _dps = 0
    _last_time = 0
    _total_sec = 0
